****STATE DATA*****
encode Name, gen(stateN)
sort stateN year
xtset stateN year

***report  panel variable:  stateN (unbalanced)
** time variable:  year, 1100 to 1790, but with gaps

generate papalconflict = .
replace papalconflict = 1 if conflicts_RA_forward_papal >0
replace papalconflict = 0 if conflicts_RA_forward_papal==0

gen Fsecularconflict = DO_dummy_all150_share[_n+30]
order Fsecularconflict, after(DO_dummy_all150_share)

gen Fpapalconflict=conflicts_RA_forward_papal[_n+30]
order Fsecularconflict, after (conflicts_RA_forward_papal)

*change number of HRE cities to HRE dummy
gen HRE=.
replace HRE=1 if HRE_Nuessli>=1 |HRE_Cantoni>=1
replace HRE=0 if HRE_Nuessli==0 |HRE_Cantoni==0

**************recode KSP to be used as in Kokkonen Sundell 2014
gen KSP=.
replace KSP=1 if KS_primogeniture >0
replace KSP=0 if KS_primogeniture ==0

*****generate Medieval dummy
gen Med=.
replace Med=1 if year<=1400
replace Med=0 if year>=1450  

label var DO_dummy_all150_share "secular conflicts"
label var conflicts_RA_forward_papal "papal conflict"
label var BishopricsDens "bishops"
label var CitiesBairochAllDens "urbanization"
label var Fpapalconflict "papal conflict lead"
label var Fsecularconflict "secular conflict lead"
label var DO_commune_mean "communes"
label var KSP "primogeniture"
label var CitiesBairochAllN "urbanization"
label var ParliamentsN "parliaments"
label var Rubin_prot "protestant"


****FROM STATE, DROP ALL NON WESTERN XTIAN 1/21/2022
*use "/Users/amgbusse/Dropbox/Church and State/City and State Data work/DataStateFinalJan22.dta"
drop if Name=="Albania"
drop if Name=="Almoravids"
drop if Name=="Astrakhan Khanate"
drop if Name=="Bulgaria"
drop if Name=="Byzantine Empire"
drop if Name=="Kazan Khanate"
drop if Name=="Chernigov"
drop if Name=="Georgia"
drop if Name=="Novgorod"
drop if Name=="Ryazan"
drop if Name=="Tarasp"
drop if Name=="Ak Koyunlu"
drop if Name=="Volga Bulgars"
drop if Name=="Serbia"
drop if Name=="Smolensk"
drop if Name=="South Slavs"

   

 save "/Users/.../FragmentationState.dta"
file /Users/.../FragmentationState.dta




*****RASTER DATA******
*drop if not Western Christian
drop if country_Russia==1
drop if country_Greece==1
drop if country_Serbia==1
drop if country_Romania==1
drop if country_Ottoman==1
drop if country_Bulgaria==1
 
*********Get rid of maritime cells
drop if NState==0
rename NState stateN
sort ID year
xtset ID year

*******generate future lag placebo variables
gen Fsecularconflict = DOdummyall150N[_n+30]
gen Fpapalconflict=conflicts_RA_forward_papal[_n+30]

*change number of HRE cities to HRE dummy
gen HRE=.
replace HRE=1 if HRE_Nuessli>=1 |HRE_Cantoni>=1
replace HRE=0 if HRE_Nuessli==0 |HRE_Cantoni==0

 ******create Primogeniture dummy****
gen KSP=.
replace KSP=1 if KS_primogeniture >0
replace KSP=0 if KS_primogeniture ==0

label var DOdummyall150N "secular conflict"
label var conflicts_RA_forward_papal "papal conflict"
label var ParliamentsN "parliaments"
label var CitiesBairochAllN "urbanization"
label var VBBCommuneN "communes"
label var KSP "primogeniture"
label var CitiesBosker5kN "urbanization"
label var Rubin_prot "protestant (Rubin)"
label var UniversitiesAllN "university"
label var stateN "fragmentation"
label var BishopricsN "bishops"
label var Fpapalconflict "papal conflict lead"

rename Cantoni_protestant_at_t protestant
rename Cantoni_protestant_at_t_share protestantshare

generate century = .
 replace century = 10 if (year >=900) & (year <=999)
 replace century = 11 if (year >=1000) & (year <=1099)
 replace century = 12 if (year >=1100) & (year <=1199)
 replace century = 13 if (year >=1200) & (year <=1299)
 replace century = 14 if (year >=1300) & (year <=1399)
 replace century = 15 if (year >=1400) & (year <=1499)
 replace century = 16 if (year >=1500) & (year <=1599)
 replace century = 17 if (year >=1600) & (year <=1699)
 replace century = 18 if (year >=1700) & (year <=1799)
 replace century = 19 if (year >=1800) & (year <=1899)
 
 
***********create papal conflict dummy
gen PCD=.
replace PCD=1 if conflicts_RA_forward_papal>=1
replace PCD=0 if conflicts_RA_forward_papal==0

gen PCDummy=.
replace PCDummy=1 if conflicts_RA_forward_papal>=1 | conflicts_RA_forward_papal<=4
replace PCDummy=0 if conflicts_RA_forward_papal==0

save "/Users/.../FragmentationRaster.dta"
file /Users/.../FragmentationRaster.dta
 